package perf.graph;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;

import org.apache.commons.collections15.functors.ConstantTransformer;

import perf.graph.format.VertexLabelTransformer;

import edu.uci.ics.jung.algorithms.layout.ISOMLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.visualization.VisualizationImageServer;
import edu.uci.ics.jung.visualization.renderers.GradientVertexRenderer;
import edu.uci.ics.jung.visualization.renderers.VertexLabelAsShapeRenderer;

public class VisualizationGenerator {

	public static VisualizationImageServer<Vertex, Edge> getImageServer( Graph<Vertex, Edge> graph, Dimension size) {
		Layout<Vertex, Edge> layout = new ISOMLayout<Vertex, Edge>(graph);

		//we use the getLabel method of the Vertex nodes for getting the name
		VisualizationImageServer<Vertex, Edge> visualizer = new VisualizationImageServer<Vertex, Edge>( layout, size );
		
		setVertexNames( visualizer );
		
		setColors( visualizer );

		
		visualizer.getRenderContext().setVertexLabelTransformer( new VertexLabelTransformer() );

		return visualizer;
	}
	
	public static void setVertexNames( VisualizationImageServer<Vertex, Edge> visu) {
		//this one will create the vertex in the shape and size needed for the label
		VertexLabelAsShapeRenderer<Vertex, Edge> vlasr = new VertexLabelAsShapeRenderer<Vertex, Edge>(
				visu.getRenderContext());
		visu.getRenderContext().setVertexShapeTransformer(vlasr);
		visu.getRenderer().setVertexLabelRenderer(vlasr);
	}
	
	@SuppressWarnings("unchecked")
	public static void setColors( VisualizationImageServer<Vertex, Edge> visu ) {
		//lets set some colors on the edges
		visu.getRenderContext().setEdgeDrawPaintTransformer(
				new ConstantTransformer(Color.blue));
		visu.getRenderContext().setEdgeStrokeTransformer(
				new ConstantTransformer(new BasicStroke(2.5f)));		
		
		// customize the renderer
		visu.getRenderer().setVertexRenderer( new GradientVertexRenderer<Vertex, Edge>(Color.lightGray,
						Color.lightGray, true));

	}
	
}
